// searchMapLocation.js
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tips: [],
    istext: true,
    searchKey: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.didSelectCell();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    wx.hideKeyboard();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  bindKeyInput: function(e) {
    var that = this;
    that.setData({
      searchKey: e.detail.value
    });
    that.clickSearch();
  },

  loseInputBlur: function() {
    var that = this;
    var keywords = that.data.searchKey;
    if (keywords == '') {
      wx.showModal({
        title: '请输入搜索内容',
        confirmColor: '#56BDE0',
        showCancel: false
      });
      that.setData({ tips: '' });
      return;
    } else {
      that.clickSearch();
    }
  },

  clickSearch: function(e) {
    var that = this;
    var keywords = that.data.searchKey;
    // console.log("keywords", keywords);
    // if (keywords == "") {
    //   wx.showModal({
    //     title: '请输入搜索内容',
    //     confirmColor: '#56BDE0',
    //     showCancel: false,
    //   })
    //   return;
    // }
    var qqmapsdk = new QQMapWX({
      // key: 'GV7BZ-RWP3W-Y52RR-RPYDN-6FWLZ-QXFQT'
      key: 'UUPBZ-T3H3D-EQF45-P3QBF-MKCAE-IOFLX'
    });
    qqmapsdk.getSuggestion({
      keyword: keywords,
      success: function(res) {
        //console.log('sucess',res);
      },
      fail: function(res) {
        //console.log('fail',res);
      },
      complete: function(res) {
        //console.log('complete',res);
        that.setData({
          tips: res.data
        });
        if (that.data.tips == []) {
          wx.showModal({
            title: '没有找到您想要的结果',
            confirmColor: '#E75858',
            showCancel: false
          });
        }
      }
    });
  },

  didSelectCell: function(e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    var index = e.currentTarget.dataset.index;
    var locationData = this.data.tips[index];
    console.log('当前选择地址为', this.data.tips[index]);
    var latitude = locationData.location.lat;
    var longitude = locationData.location.lng;
    prevPage.setData({
      detailAddress: locationData.title,
      address:
        locationData.province +
        ' ' +
        locationData.city +
        ' ' +
        locationData.district,
      location: latitude + ',' + longitude,
      lat: latitude,
      lng: longitude
    });
    var locationDic = { latitude: latitude, longitude: longitude };
    wx.setStorage({
      key: 'map_Location',
      data: locationDic
    });
    wx.navigateBack({
      delta: 1
    });
  }
});
